package com.woniuxy.controller;

import com.woniuxy.pojo.Permission;
import com.woniuxy.service.PermissionService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.PermissionVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PermissionController {
    private Logger logger= LoggerFactory.getLogger(PermissionController.class);
    @Autowired
    private PermissionService permissionService;


    @GetMapping(value = "/permissions")
    public DataGridView selectPermissionsByPage(PermissionVo permissionVo){
        return permissionService.getPermissionListByPage(permissionVo);
    }


    @GetMapping(value ="permission/selectAllPermission")
    public List<Permission> selectAllPermission(){
        List<Permission> permissions=permissionService.selectAllPermission();//得到所有权限
        return permissions;
    }

    @GetMapping(value = "permission/getChecked")
    public Integer[] getChecked(int rid){

       return permissionService.selectPerIdByRid(rid);//先通过角色id得到对应权限，然后在得到角色对应的权限id
    }

    @GetMapping(value = "permission/assignRight")
    public ResultDto assignRight(int rid, @RequestParam("perids[]") Integer[] perids){
        try {
            permissionService.assignRight(rid,perids);
            return ResultDto.RIGHT_GRANT_SUCCESS;
        }catch (Exception e){
            return ResultDto.RIGHT_GRANT_FAILURE;
        }
    }




}
